package cn.labbook.bluetooth.util;

import cn.labbook.bluetooth.entity.alipay.AlipayConfig;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2019/4/10.
 */
public class AlipayUtil {
//    private String APP_ID="2018052160198043";
//    private String APP_PRIVATE_KEY="D4dqVIdnCRFp33TDwOmaTZa+7wM3ZFPQpckB2XekmtKJX7h2GJqb8NqczjF7u1Q4Ga3d3u+RPwKBgGxcIRXA2tt0+jLKVLywrBc3vGKePBmtnbqsKRiEshE0izPALwfG8/m4GvsXt9c1ibqzWjhLL852OZ9d4VLGN77GvLA35R1wwI+wpjELh/HSwtoMI4QGFv2R/STmxeFyo9R8OCVXMmfriJQvia/vHsbNaWvGxFM/M0V5QDUP0Dj7AoGAI63FbPgn4PdXT9vwiR/+Qt1KS09y8lmu3eXp7gI4pRg2d0jztKhCT+wfBGVenWy5i+3XCpp81LmGtpULE6nxv+tyaeksPjPKPAUltmf6o86jxHCpgxAAFdNtgAauTA7LybFhTlHyn10CG8UzciSmlBuQ/KSzNynXIb0xFJJeXQcCgYEA45nrnkqAplDXxopGiuK3SGbbJ9eAO7FTr9rHjBMDqbOAIxAwN+pNnZ6WqWxBtXe5Tx1tKH++x/Y42DgvPuW2J0KCEyr/uRHbuNmaaxvf2wga43+UMAI+Okl1mUK1mEmZtOwPaRDpOOz01YFD3jtlXXfkubCX5YqdfEDaOoO6Oyc=";
//    private String ALIPAY_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgvZXPZOIrC2mdR2Iz8zUtiiumCKwWxVZjn9p6YJfLGyoVdcDm9LSDnMu8Ybo16PE200cHZYtmYlKyDOEuOZv4wPK8a/E4Qfy3taqrUDqZHrXfdzeoNpj76+R374SGkdr9a9HinMtKjxQSqevwaNLceEle8jizLvipRkU3rppv/4JbrCrLvY9gFaD5LIhVb9njOawFtUX46YNsZmJPcuxE81z+dIbpRWmIPRfvUIp47Y/xPnuXhTE9nZIMIRA+jVNC785T1QPumEAZNlktOYkeF3pm0vrAC8T3aOvRwCd06ALfYcOZoKkKMiD298JO4YH+wzH8BXqGOpn9TXtzUnTdQIDAQAB";
//    private String FORMAT="json";
//    private String CHARSET ="UTF-8";
//    private String SIGN_TYPE = "RSA2";

    public static AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //获得初始化的AlipayClient

    public HttpServletResponse doPost(HttpServletRequest httpRequest,
                                             HttpServletResponse httpResponse) throws ServletException, IOException {

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//创建API对应的request
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                "\"out_trade_no\":\"20150320010101001\"," +
//                "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
                "\"total_amount\":0.01," +
                "\"subject\":\"Iphone6 16G\"," +
                "\"body\":\"Iphone6 16G\"," +
                "\"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," +
                "\"extend_params\":{" +
                "\"sys_service_provider_id\":\"2088511833207846\"" +
                "}"+
                "}");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + AlipayConfig.charset);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
        return httpResponse;
    }
}
